<?php

namespace api\controller;

// +----------------------------------------------------------------------
// | 租户授权管理接口控制层
// +----------------------------------------------------------------------

use common\controller\VerifyController;
use common\exception\LogicException;
use common\service\TenantGrantService;
use think\Request;

class TenantGrantController extends VerifyController
{
    /**
     * @var TenantGrantService
     */
    private $tenantGrantService;

    public function __construct()
    {
        parent::__construct();
        $this->tenantGrantService = new TenantGrantService();
    }


    /**
     * 授权自动签署
     * @param Request $request
     * @return \think\Response
     * @throws \Exception
     */
    public function automaticSignature(Request $request)
    {
        $param = $request->param();
        $res = $this->tenantGrantService->authorizedAutomaticSignature($param['param'], $request->getTenantId());
        return json(success_response("", $res));
    }

    /**
     * 授权自动签署 状态查询
     * @param Request $request
     * @return \think\Response
     */
    public function automaticSignatureStatus(Request $request)
    {
        $param = $request->param();
        $res = $this->tenantGrantService->automaticSignatureStatus($param['param']);
        return json(success_response("", $res));
    }

    /**
     * 查询
     * @param Request $request
     * @return \think\Response
     */
    public function select(Request $request)
    {
        $tenantId = $request->param()['param']['tenant_id'] ?? $request->getTenantId();
        $res = $this->tenantGrantService->select($request->param(), $tenantId);
        return json(success_response("OK", $res));
    }

    /**
     * 删除授权
     * @param Request $request
     * @return \think\Response
     */
    public function delete(Request $request)
    {
        try {
            $res = $this->tenantGrantService->delete($request->param());
            return json(success_response("OK", $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }
}